﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using IMU6DOFV2.Netduino;
using XBee.Library;
using XBee.Communications;
using VEarth;
using VEarth.Locations;

namespace UAV.OnGround
{
    public partial class frmMain : Form
    {
        List<SearchLocation> locations = new List<SearchLocation>();

        public frmMain()
        {
            InitializeComponent();
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            toolStripComboBox1.Items.AddRange(SerialPort.GetPortNames());
        }

        private void btnConnect_Click(object sender, EventArgs e)
        {
            XBEE _xbee = new XBEE(toolStripComboBox1.SelectedItem.ToString(), new IMUData());
            _xbee.OnReciveData += new ReciveDataDelegate(_xbee_OnReciveData);
        }

        private void _xbee_OnReciveData(ICommunicationData data)
        {
            txtLog.Invoke(new MethodInvoker(delegate() { txtLog.Text += data.Serialize() + Environment.NewLine; }));
        }

        private void frmMain_Shown(object sender, EventArgs e)
        {
            ucVEarth.InitMap();    // Initialize the VEarth user control
            ucVEarth.VE_OnMouseClick += new MouseClick(ucVEarth_VE_OnMouseClick);
            SearchLocation center = new SearchLocation();
            center.Latitude=40.5091660504084;
            center.Longitude=-3.6702066994628;
            ucVEarth.VE_SetCenter(center);
        }

        private void ucVEarth_VE_OnMouseClick(double latitude, double longitude, object id, bool altKey, bool ctrlKey, bool shiftKey, bool leftButton, bool rightButton)
        {
            if (altKey)
            {
                SearchLocation loc = new SearchLocation();
                loc.Latitude = latitude;
                loc.Longitude = longitude;
                loc.PushPinDescription = String.Format("Lat: {0}, Long: {1}", latitude, longitude);
                loc.PushPinTitle = "You've put me on the map";
                ucVEarth.VE_AddPushPin(loc);
                locations.Add(loc);

            }
        }

        private void btnDisplayLine_Click(object sender, EventArgs e)
        {
            if (locations.Count > 0)
            {
                locations.Add(locations[0]);
                ucVEarth.VE_AddShape(VE_Shape.Polyline, locations, "", "", "", 2);
            }
        }

        private void clearAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.txtLog.Clear();
        }
    }
}
